import { selCountByCurrentUser, selCountByUserId, 
    selCountErrByCurrentUser, selCountErrByUserId, 
    selTotalByCurrentUser, selTotalByUserId} from '@/services/count';
import { selUserByUsernameLike } from '@/services/user';
import moment from 'moment';

export default {

    namespace: 'count',
    
    state: {
        userCountList: [],
        userCountErrList: [],
        userList: [],
        total: 0,
        totalErr: 0,
        longTotal:0, //长短信统计
        shortTotal:0, //短短信统计
        type: "month"
        
    },
    
    effects: {
        // *fetchSelShortAndLongTotalAll(param, { call, put }) { //查询全平台长短短信统计
        //     const response = yield call(selShortAndLongTotalAll, param);
        //     if(response.success){
        //         yield put({
        //             type: 'setShortAndLongTotal',
        //             payload: response.data
        //         });
        //     }
        // },
        // *fetchSelShortAndLongTotalByCurrentUser(param, { call, put }) { //查询当前用户长短短信统计
        //     const response = yield call(selShortAndLongTotalByCurrentUser, param);
        //     if(response.success){
        //         yield put({
        //             type: 'setShortAndLongTotal',
        //             payload: response.data
        //         });
        //     }
        // },
        *fetchSelCountByCurrentUser(param, { call, put }) {
            const response = yield call(selCountByCurrentUser, param);
            const response2 = yield call(selCountErrByCurrentUser, param);
            const response3 = yield call(selTotalByCurrentUser, param);
            if(response.success){
                yield put({
                    type: 'setUserCountList',
                    payload: response.data
                });
            }
            if(response2.success){
                yield put({
                    type: 'setUserCountErrList',
                    payload: response2.data
                });
            }
            if(response3.success){
                yield put({
                    type: 'setTotal',
                    payload: {
                        total: response3.data.count,
                        totalErr: response3.data.countErr
                    }
                });
            }
        },
        *fetchSelCountByUserId(param, { call, put }) {
            const response = yield call(selCountByUserId, param);
            const response2 = yield call(selCountErrByUserId, param);
            const response3 = yield call(selTotalByUserId, param);
            if(response.success){
                yield put({
                    type: 'setUserCountList',
                    payload: response.data
                });
            }
            if(response2.success){
                yield put({
                    type: 'setUserCountErrList',
                    payload: response2.data
                });
            }
            if(response3.success){
                yield put({
                    type: 'setTotal',
                    payload: {
                        total: response3.data.count,
                        totalErr: response3.data.countErr
                    }
                });
            }
        },
        *fetchSelUserByUsernameLike(param, { call, put }) {
            const response = yield call(selUserByUsernameLike, param);
            if(response.success){
                yield put({
                    type: 'setUserList',
                    payload: response.data
                });
            }
        },
    },
    
    reducers: {
        setShortAndLongTotal(state, action){
            return {
                ...state,
                shortTotal: action.payload.shortTotal,
                longTotal: action.payload.longTotal
            };
        },
        setUserCountList(state, action){
            let rsList = []
            action.payload.forEach(element => {
                let mat = 'YYYY-MM-DD'
                if(state.type === "year"){
                    mat = 'YYYY'
                }
                if(state.type === "month"){
                    mat = 'YYYY-MM'
                }
                if(state.type === "day"){
                    mat = 'YYYY-MM-DD'
                }
                let rs = {
                    x: moment(element.sendTime).format(mat),
                    y: element.sendCount
                }
                rsList.push(rs)
            });
            return {
                ...state,
                userCountList: rsList,
            };
        },
        setUserCountErrList(state, action){
            let rsList = []
            action.payload.forEach(element => {
                let mat = 'YYYY-MM-DD'
                if(state.type === "year"){
                    mat = 'YYYY'
                }
                if(state.type === "month"){
                    mat = 'YYYY-MM'
                }
                if(state.type === "day"){
                    mat = 'YYYY-MM-DD'
                }
                let rs = {
                    x: moment(element.sendTime).format(mat),
                    y: element.sendCount
                }
                rsList.push(rs)
            });
            return {
                ...state,
                userCountErrList: rsList,
            };
        },
        setTotal(state, action){
            return {
                ...state,
                total: action.payload.total,
                totalErr: action.payload.totalErr
            };
        },
        setType(state, action){
            return {
                ...state,
                type: action.payload.type,
            };
        },
        setUserList(state, action){
            return {
                ...state,
                userList: action.payload,
            };
        },
    },

}